// pages/index/announceList/announceList.js
const request = require('../../../utils/request')
const utils = require('../../../utils/util')
const regex = require('../../../utils/regex')

Page({
  data: {
    show: false,
    popShow: false,
    time: '',
    distance: '',
    date: +new Date(),
    isStaff: false
  },

  onLoad() {
    this.setData({
      isStaff: utils.isStaff()
    })
  },

  navigateto(e) {
    const type = e.currentTarget.dataset.type
    console.log(type)
    const urlMap = {
      market: '../talentMarket/talentMarket/talentMarket',
      mine: '../talentMarket/myAnnounce/myAnnounce',
    }

    wx.navigateTo({
      url: urlMap[type]
    })
  },

  // 保养提醒
  // maintainRemind() {
  //   wx.showLoading()
  //   request.get('baoyang_remind')
  //     .then(res => {
  //         wx.showModal({
  //           title: '下次保养时间',
  //           content: res.data && res.data,
  //           showCancel: false
  //         })
  //     })
  //     .catch(error => {
  //       if (error.code === 1) {
  //         this.setData({
  //           show: true
  //         })
  //         return
  //       }
  //       wx.showToast({
  //         icon: 'none',
  //         title: error.msg && error.msg
  //       })
  //     })
  //     .finally(_ => {
  //       wx.hideLoading()
  //     })
  // },

  setRemind() {
    wx.showLoading()
    const data = {
      last_baoyang_time: this.data.time,
      last_baoyang_licheng: this.data.distance
    }
    request.post('set_baoyang', data)
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
        this.setData({
          show: false
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  onFocus() {
    this.setData({
      popShow: true
    })
  },

  onConfirm(e) {
    if (!this.data.time) {
      wx.showToast({
        icon: 'none',
        title: '请选择时间'
      })
      e.detail.dialog.stopLoading()
      return
    }

    if (!regex.date.test(this.data.time)) {
      wx.showToast({
        icon: 'none',
        title: '时间格式不正确'
      })
      e.detail.dialog.stopLoading()
      return
    }

    if (!this.data.distance) {
      wx.showToast({
        icon: 'none',
        title: '请填写公里'
      })
      e.detail.dialog.stopLoading()
      return
    }

    if (!regex.number.test(this.data.distance)) {
      wx.showToast({
        icon: 'none',
        title: '公里必须为数字'
      })
      e.detail.dialog.stopLoading()
      return
    }

    this.setRemind()
  },

  onCancel() {
    this.setData({
      show: false
    })
  },

  onPopClose() {
    this.setData({
      popShow: false
    })
  },

  onPickerConfirm(time) {
    this.setData({
      popShow: false,
      time: utils.parseTime(time.detail, '{y}-{m}-{d}')
    })
  },

  onDistInput(value) {
    this.setData({
      distance: value.detail
    })
  }
})
